package org.firefly.fx.util;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

public class RequestUtil {
	public static int getInt(HttpServletRequest request,String param,int defaultVal){
		if(StringUtils.isBlank(param)){
			return defaultVal;
		}
		
		String v = request.getParameter(param);
		if(!StringUtils.isNumeric(v)){
			return defaultVal;
		}
		
		return Integer.parseInt(v); 
	}
	
	public static long getLong(HttpServletRequest request,String param,long defaultVal){
		if(StringUtils.isBlank(param)){
			return defaultVal;
		}
		
		String v = request.getParameter(param);
		if(StringUtils.isNumeric(v)){
			return Long.parseLong(v); 
		}
		
		v = (String) request.getAttribute(param);
		if(StringUtils.isNumeric(v)){
			return Long.parseLong(v); 
		}
		
		return defaultVal;
	}
	
	public static String getString(HttpServletRequest request,String param,String defaultVal){
		if(StringUtils.isBlank(param)){
			return defaultVal;
		}
		
		String v = request.getParameter(param);
		if(StringUtils.isBlank(v)){
			return defaultVal;
		}
		return v;
	}
}